#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 19-5-31 下午8:09
# @Author  : Tom
# @Site    : 
# @File    : categories.py
# @Software: PyCharm

from goods.models import GoodsChannel


# 获取广告的三级分类的返回对象
def get_categories():
    '''获取广告的三级分类的返回对象'''
    # 构造结果字典
    '''
    categories={
        频道编号：{
            channels:[],一级分类 url,name
            sub_cats:[]二级分类 cat2.name, cat3.id,cat3.name
        }
    },如下列数据
    '''
    # 返回的分类字典
    categories = {}
    # 获取频道列表
    channel_list = GoodsChannel.objects.all().order_by('group_id', 'sequence')
    for channel in channel_list:
        # 获取当前的组的id
        group_id = channel.group_id
        # 添加频道
        if group_id not in categories:
            categories[group_id] = {'channels': [], 'sub_cats': []}
        # 获取第一分类的对象
        cat1 = channel.category
        # 第一级分类
        categories[group_id]['channels'].append({
            'name': cat1.name,
            'url': channel.url,
        })
        # 第二级分类
        for cat2 in cat1.subs.all():
            cat2.sub_cats =[]
            # 查看第三级分类
            for cat3 in cat2.subs.all():
                cat2.sub_cats.append(cat3)
            categories[group_id]['sub_cats'].append(cat2)
    return categories








